package com.mmc.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 文件服务控制器
 *
 * @author kunfe
 * @since 2024-01-01
 */
@Tag(name = "文件服务", description = "文件上传下载服务")
@RestController
@RequestMapping("/api/file")
@Slf4j
public class FileController {

    @Operation(summary = "获取头像文件")
    @SaIgnore
    @GetMapping("/avatar/{filename}")
    public ResponseEntity<Resource> getAvatar(@PathVariable String filename) {
        try {
            Path filePath = Paths.get("./upload/avatar/").resolve(filename).normalize();
            File file = filePath.toFile();
            
            if (!file.exists() || !file.isFile()) {
                return ResponseEntity.notFound().build();
            }
            
            Resource resource = new FileSystemResource(file);
            String contentType = Files.probeContentType(filePath);
            if (contentType == null) {
                contentType = "application/octet-stream";
            }
            
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(contentType))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"")
                    .body(resource);
        } catch (Exception e) {
            log.error("获取头像文件失败: {}", filename, e);
            return ResponseEntity.notFound().build();
        }
    }
    
    @Operation(summary = "获取上传文件")
    @SaIgnore
    @GetMapping("/upload/{type}/{filename}")
    public ResponseEntity<Resource> getUploadFile(@PathVariable String type, @PathVariable String filename) {
        try {
            Path filePath = Paths.get("./upload/" + type + "/").resolve(filename).normalize();
            File file = filePath.toFile();
            
            if (!file.exists() || !file.isFile()) {
                return ResponseEntity.notFound().build();
            }
            
            Resource resource = new FileSystemResource(file);
            String contentType = Files.probeContentType(filePath);
            if (contentType == null) {
                contentType = "application/octet-stream";
            }
            
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(contentType))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"")
                    .body(resource);
        } catch (Exception e) {
            log.error("获取上传文件失败: {}/{}", type, filename, e);
            return ResponseEntity.notFound().build();
        }
    }
}